#!/bin/bash
# $Id$

# desc: output /var/tmp/sd[a-z][0-9] as text file, in it desc and size 
# 			of the device, and to stdout

export syspath=/sys/block
# ex. usblist="sda1 sdb"
export usblist=""
export tmpmnt="`mktemp -d /tmp/tmp.XXXXXX`"


find_dev_m1 ()
{
	for l in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
		if [ -d "/sys/block/sd${l}" ]; then
			mdev -s
			sfdisk -R /dev/sd${l} > /dev/null 2>&1
			mdev -s
			ls -l $syspath/sd${l}/|grep -q usb 2> /dev/null
			if [ "$?" -eq "0" ]; then
				usblist="${usblist} `_test_mount $l`"
			fi
		fi
	done

	if [ -z "$usblist" ]; then
		return 1
	else
		echo ${usblist} 
	fi
}

# param: 1:letter
_test_mount () 
{
	local l=$1
	for n in 1 2 3 4 5 6 7 8 9; do
		mount -o ro /dev/sd${l}${n} $tmpmnt >/dev/null 2>&1
		if [ "$?" -eq "0" ]; then 
			echo sd${l}${n}
		fi
		umount /dev/sd${l}${n} $tmpmnt >/dev/null 2>&1
	done
}

id_dev ()
{
	local V
	local M
	local S
	local tmpdir="/var/tmp"

	for u in $usblist; do
		V="$(< $syspath/`echo $u|tr -d [1-9]`/device/vendor)"	
		M="$(< $syspath/`echo $u|tr -d [1-9]`/device/model)"	
		#S="`sfdisk -uM -l|grep $u|awk '{print $4}'|tr -d "-"`"
		S="`fdisk -l /dev/${u} 2>/dev/null |grep -E "GB|MB" |cut -f2 -d":"|cut -f1 -d","`"
		#
		echo "Device: $u" > $tmpdir/usb_${u}
		echo "Brand: $V" >> $tmpdir/usb_${u}
		echo "Model: $M" >> $tmpdir/usb_${u}
		#echo "Size: ${S} MB" >> $tmpdir/usb_${u}
		echo "Size: ${S}" >> $tmpdir/usb_${u}
	done
}

mdev -s
find_dev_m1 && id_dev

# vim: ts=2 bs=2
# lookfor-sd-devs.sh: end
